<?php require_once('../src/session_start.inc.php'); ?>
<?php
require_once('../src/functions.php');
require_once('../src/annonce.class.php');
require_once('../lib/uploader.class.php');
require_once('../lib/createImage.inc.php');
require_once('../lib/fileManager.inc.php');

// on verifie qu'il y a un id d'annonce
if (!isset($_REQUEST['id_annonce']) OR $_REQUEST['id_annonce']=='') {
    log_error("L'upload de photo d'annonce BUG (id d'annonce manquant)");
    header('Location: annonce.php');
}
$id_annonce = $_REQUEST['id_annonce'];
$annonce = new annonce($id_annonce);

$photo = new photo();

$photo_relative_path = PHOTO_HTTP_DIR;
$photo_absolute_path = realpath('../' . PHOTO_DIR) . '/';


$error = false;

// supression d'une image
if (isset($_GET['action']) && $_GET['action']=='delete') {
    $photo->id = $_GET['id_photo'];
    $photo->reload();
    $annonce->deletePhoto($photo);
    header("Location: annonce_photo.php?id_annonce=" . $annonce->id);
}

if (isset($_POST['action']) && $_POST['action']!='') {
if ($_POST['action']=='upload') {
    $upload = new uploader('upload');
	if (!$upload->copyFile('../' . TMP_FILE_DIR)) {
        log_error("Erreur d'upload > " . $upload->error . "\nObject Uploader: " . npr($upload));
        $error = "Erreur dans l'upload : " . $upload->error;
    } else {
		$filename = $upload->issue_filename;
		$tmp_absolute_path = $upload->issue_absolute_path;
		$photo_filename = $annonce->id . '_' . $filename;
        
        $createImage = true;
        // redimensionement de la photo
        if ( !createImage($tmp_absolute_path.$filename, $photo_absolute_path.$photo_filename, PHOTO_SIZE) ) {
            log_error("Erreur de redimensionnement de l'image > " . $photo_relative_path . $photo_filename);
            $error = "Une erreur est survenue, veuillez repéter l'opération.";
            $createImage = false;
        }
        // creation de la minuature
        if ( !createImage($tmp_absolute_path.$filename, $photo_absolute_path.THUMB_PREFIX.$photo_filename, THUMB_SIZE) ) {
            log_error("Erreur de creation de la vignette > " . $photo_relative_path . THUMB_PREFIX . $photo_filename);
            $error = "Une erreur est survenue, veuillez repéter l'opération.";
            $createImage = false;
        }

        if ($createImage===true) {
            deleteFile($tmp_absolute_path.$filename);
            
            $photo->filename = $photo_filename;
            $photo->description = $_POST['description'];
            $annonce->addPhoto($photo);
            
            header('Location: ' . $_SERVER['PHP_SELF'] . '?id_annonce=' . $id_annonce);
        }
    }
} elseif ($_POST['action']=='edit') {
    $photo->id = $_POST['id_photo'];
    $photo->description = $_POST['description'];
    $photo->save();
    header('Location: ' . $_SERVER['PHP_SELF'] . '?id_annonce=' . $id_annonce);
}
}
//npr($annonce);
?>
<?php require_once('admin_header.php'); ?>

<h2>Annonce - ref. <?php echo $annonce->reference ?></h2>

<p><a href="annonce_show.php?id=<?php echo $id_annonce ?>"><?php echo getIcon('application_form_magnify') ?>Voir le détail de cette annonce</a></p>

<p class="error"><?php echo $error; ?></p>

<?php for($i=0; $i<NB_PHOTO; ++$i): ?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>" enctype="multipart/form-data" id="upload_form_<?php echo $i ?>" name="upload_form_<?php echo $i ?>">
  <fieldset>
    <legend>Photo <?php echo $i+1; ?></legend>
<?php
if (isset($annonce->photos[$i])) {
    $action = 'edit';
    echo "\n<p>";
    echo '<a href="javascript:open_popup(\'' . $photo_relative_path . $annonce->photos[$i]->filename . '\');"><img src="' . $photo_relative_path . THUMB_PREFIX . $annonce->photos[$i]->filename . '" alt="" /></a>';
    echo '<a href="' . $_SERVER['PHP_SELF'] . '?id_photo=' . $annonce->photos[$i]->id . '&id_annonce=' . $id_annonce . '&action=delete">supprimer cette photo</a>';
    echo '<input type="hidden" name="id_photo" value="' . $annonce->photos[$i]->id . '" />';
    echo "</p>\n";
} else {
    $action = 'upload';
?>
    <p>
        <input type="file" name="upload" id="upload_<?php echo $i ?>" size="30" readonly="readonly" accept="image/*" />
    </p>
<?php } ?>
    <p>
        <label for="description_<?php echo $i ?>">Description : </label>
        <input type="text" id="description_<?php echo $i ?>" name="description" value="<?php if(isset($annonce->photos[$i]->description)) echo $annonce->photos[$i]->description ?>" />
    </p>
    <p>
        <input type="hidden" name="action" value="<?php echo $action; ?>" />
        <input type="hidden" name="id_annonce" value="<?php echo $id_annonce ?>" />
        <input type="submit" value="Envoyer" />
    </p>
  </fieldset>
</form>

<?php endfor; ?>

<p><a href="annonce.php"><?php echo getIcon('application_view_list') ?>Retour à la liste des annonces</a></p>

<?php require_once('admin_footer.php'); ?>